"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:ContractFbcontractCtrl
 * @description
 * # ContractFbcontractCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller("ContractFbcontractCtrl", function(
    $scope,
    $location,
    openApi,
    Util,
    $routeParams,
    FormSubmit,
    localStorageService
  ) {
    var id = ($scope.process_id = $routeParams.id);
    // 税率
    $scope.rateList = [{
      key: "无",
      value: "无"
  }, {
      key: "1%",
      value: "1%"
  }, {
      key: "2%",
      value: "2%"
  }, {
      key: "3%",
      value: "3%"
  }, {
      key: "5%",
      value: "5%"
  }, {
      key: "6%",
      value: "6%"
  }, {
      key: "9%",
      value: "9%"
  }, {
      key: "10%",
      value: "10%"
  }, {
      key: "11%",
      value: "11%"
  }, {
      key: "13%",
      value: "13%"
  }, {
      key: "16%",
      value: "16%"
  }, {
      key: "17%",
      value: "17%"
  }, {
      key: "免税",
      value: "免税"
  }, {
      key: "其他",
      value: "其他"
  }];
    // 发票类型
    $scope.invoiceTypeList = [
      {
        key: "增值税普通发票",
        value: "增值税普通发票"
      },
      {
        key: "增值税专用发票",
        value: "增值税专用发票"
      },
      {
        key: "普通商业发票",
        value: "普通商业发票"
      },
      {
        key: "其它",
        value: "其它"
      }
    ];
    $scope.vatlist=[{key:"含税",value:1},{key:"不含税",value:0}];
    $scope.chooseProject = function() {
      // 将列表的数据序列化存入缓存以便回来时候使用
      Util.savaParamForReview($scope.param, id);
      $location.path("project/chooseproject");
    };

    $scope.chooseSupplier = function() {
      var params = $scope.param;
      var projectParams = {
        formKey: "Xm_SupplierStore",
        fieldName: "supplierNo"
      };

      // 清掉关联的编号
      Util.savaParamForReview(params, id);
      localStorageService.set("projectParams", projectParams);
      $location.path("/project/choosedata");
    };

    $scope.submit = function(operationId) {
      // 底部类型end
      var options = {};
      // 流程id
      options.id = id;
      // 根据operationId判断是草稿还是提交
      options.operationId = operationId;
      options.urlParam = openApi.contractsubpackage;
      options.param = angular.copy($scope.param, {});
      if(options.param.relevances){
        options.param.relevanceIds = options.param.relevances.map(function(item) {
          return item.targetId;
        });
      }
      if(options.param.invoiceRevs){
        options.param.invoiceRevIds = options.param.invoiceRevs.map(function(item) {
          return item.invoiceId;
        });
      }
      delete options.param.invoiceRevs;
      delete options.param.relevances;
      FormSubmit.submit(options,$scope);
    };
    $scope.validate = function() {
      if (!$scope.param.projectName) {
        Util.Toast("请选择项目");
        return;
      }
      if (!$scope.param.contractName) {
        Util.Toast("请填写合同名称");
        return;
      }
      if (!$scope.param.contractType) {
        Util.Toast("请选择分包类型");
        return;
      }
      if (!$scope.param.contractMode) {
        Util.Toast("请选择分包模式");
        return;
      }
      if (!$scope.param.partA) {
        Util.Toast("请填写甲方");
        return;
      }
      if (!$scope.param.partB) {
        Util.Toast("请填写乙方");
        return;
      }
      if (!$scope.param.invoiceType) {
        Util.Toast("请选择发票类型");
        return;
      }
      if (!$scope.param.taxRate) {
        Util.Toast("请选择税率");
        return;
      }
      if (!$scope.param.taxInclusive&&$scope.param.taxInclusive!==0) {
        Util.Toast("请选择是否含税");
        return;
      }
      if (!$scope.param.contractAmount&&$scope.param.contractAmount!==0) {
        Util.Toast("请输入合同金额");
        return;
      }

      if (!$scope.validation.passvalidation) {
        Util.Toast($scope.validation.tip);
        return;
      }
      return true;
    };
    var init = function() {
      // 获取回显的详细信息
      Util.getDetailData(id, function(data) {
        $scope.param = data;
        var data = localStorageService.get("projectinfo");
        localStorageService.remove("projectinfo");
        // 获取项目关联数据
        var projectreldata = localStorageService.get("projectreldata");
        localStorageService.remove("projectreldata");
        $scope.param = Object.assign({}, $scope.param, data, projectreldata);
        $scope.param.details = $scope.param.details || [];
      });
    };
    // 初始化
    init();
  });
